package com.lune.blog.auth.controller;


import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.extra.servlet.ServletUtil;
import cn.hutool.http.server.HttpServerRequest;
import cn.hutool.jwt.JWTUtil;
import com.lune.blog.api.feign.UserServiceFeign;
import com.lune.blog.auth.domain.LoginBody;
import com.lune.blog.auth.service.AdminLoginServiceImpl;
import com.lune.blog.base.domain.LoginUser;
import com.lune.blog.base.result.CommonResult;
import com.lune.blog.base.result.R;
import com.lune.blog.base.utils.ServletUtils;
import com.lune.blog.base.utils.ip.IpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


/**
 * @author yanjizhw@163.com
 * @date 2021/11/2 22:43
 * 描述:
 * @since 1.0.0
 */
@RestController
@RequestMapping("/admin")
public class AdminLoginController {

    @Resource
    private AdminLoginServiceImpl adminLoginService;

    @PostMapping("/doLogin")
    public R<?> doLogin(@RequestBody LoginBody loginBody) {
        LoginUser loginUser =  adminLoginService.login(loginBody.getUsername(), loginBody.getPassword());
        StpUtil.login(loginUser.getSysUser().getUserId());
        String tokenValue = StpUtil.getTokenValue();
        loginUser.setToken(tokenValue);
        String ipAddr = IpUtils.getIpAddr(ServletUtils.getRequest());
        loginUser.setIpaddr(ipAddr);
        return R.ok(loginUser, "登录成功");
    }
}
